﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.PhongMau
{
    public partial class Phongmau_Themloaihang : Form
    {
        public static string MaID = "0";
        public bool update = false;

        public Phongmau_Themloaihang()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            if (MaID != "0")
            {
                var data = DataAll.SelectAll("PhongMau_LoaiHang", "*", " WHERE IDLoaiHang=" + MaID);
                txtMaLoai.Text = data.Rows[0]["MaLoaiHang"].ToString();
                txtTenLoai.Text = data.Rows[0]["TenLoaiHang"].ToString();
                txtGhiChu.Text = data.Rows[0]["GhiChu"].ToString();
            }
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void ResetControl()
        {
            txtMaLoai.Text = string.Empty;
            txtTenLoai.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
        }

        private void bntLuuLai_Click(object sender, EventArgs e)
        {
            if (update)
            {
                var message = DataAll.Update("PhongMau_LoaiHang", "MaLoaiHang=N'" + txtMaLoai.Text + "', TenLoaiHang=N'" + txtTenLoai.Text
                    + "', GhiChu=N'" + txtGhiChu.Text + "' WHERE IDLoaiHang=" + MaID);
                MessageBox.Show(message.Equals("success") ? "Sửa nhóm hàng thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Close();
            }
            else
            {
                var message = DataAll.InsertAll("PhongMau_LoaiHang", txtMaLoai.Text + "," + txtTenLoai.Text + "," + txtGhiChu.Text);
                MessageBox.Show(message.Equals("success") ? "Thêm loại hàng thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                ResetControl();
            }
        }
    }
}
